package sany.iec.service.olp01;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import sany.iec.common.GlobalConstant;
import sany.iec.config.IecEmsServer;
import sany.iec.config.IecOlp01Server;
import sany.iec.entity.IECData;
import sany.iec.service.IecSendMessage;
import sany.iec.service.IecServerService;
import sany.iec.service.ems.EMSDataService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : 托马斯
 * @Date : 2025/7/2 16:58
 * @Description :
 */
@Slf4j
@Service
public class OLP01Schedule {

    @Resource(name = "OLP01")
    private IecServerService iecServerService;

    @Resource
    private OLP01DataService olpDataService;

    @Resource
    private EMSDataService emsDataService;

    @Resource
    private IecSendMessage iecSendMessage;

    @Resource
    private IecEmsServer iecEmsServer;

    @Scheduled(initialDelay = 5000, fixedRate = 3000)
    public void insertData() {
        Map<Integer, Float> map = GlobalConstant.RECEIVE_DATA.getOrDefault(iecEmsServer.getPort(), new HashMap<>());
        if (map.containsKey(25089)) {
            // 说明存在有功 agcValue 指令
            Float v = map.get(25089);
            if (!olpDataService.updateOrigin("OutLinePower", emsDataService.getPowerSum_RTValue() * 0.96f)) {
                log.warn("修改失败~~~   key: {}", "OutLinePower");
            }
            if (!olpDataService.updateOrigin("OutLinePower2", emsDataService.getPowerSum_RTValue() * 0.96f)) {
                log.warn("修改失败~~~   key: {}", "OutLinePower2");
            }
            if (!olpDataService.updateOrigin("CollectionPSum", emsDataService.getPowerSum_RTValue() * 0.96f)) {
                log.warn("修改失败~~~   key: {}", "CollectionPSum");
            }
        }
        if (map.containsKey(25090)) {
            // 说明存在有功 avcValue 指令
            // 无功 = ems无功 + svg无功
            Float avc = map.get(25090) + GlobalConstant.SVG_VALUE;
            log.info("ems无功：{}Kvar  svg无功: {}Kvar  总无功: {}Kvar", map.get(25090), GlobalConstant.SVG_VALUE, avc);

            avc = avc / 1000.0f;

            if (!olpDataService.updateOrigin("OutLineRepower", avc)) {
                log.warn("修改失败~~~   key: {}", "OutLineRepower");
            }
            if (!olpDataService.updateOrigin("OutLineRepower2", avc)) {
                log.warn("修改失败~~~   key: {}", "OutLineRepower2");
            }

            // 计算得到电压
            Float VOLTAGE_115 = GlobalConstant.RATE * avc + GlobalConstant.RATED_VOLTAGE;

            // 根据无功进行电压的变换
            if (!olpDataService.updateOrigin("OutLineUab", VOLTAGE_115)) {
                log.warn("修改失败~~~   key: {}", "OutLineUab");
            }
            if (!olpDataService.updateOrigin("OutLineUbc", VOLTAGE_115)) {
                log.warn("修改失败~~~   key: {}", "OutLineUbc");
            }
            if (!olpDataService.updateOrigin("OutLineUca", VOLTAGE_115)) {
                log.warn("修改失败~~~   key: {}", "OutLineUca");
            }
            if (!olpDataService.updateOrigin("OutLineUab2", VOLTAGE_115)) {
                log.warn("修改失败~~~   key: {}", "OutLineUab2");
            }
            if (!olpDataService.updateOrigin("OutLineUbc2", VOLTAGE_115)) {
                log.warn("修改失败~~~   key: {}", "OutLineUbc2");
            }
            if (!olpDataService.updateOrigin("OutLineUca2", VOLTAGE_115)) {
                log.warn("修改失败~~~   key: {}", "OutLineUca2");
            }

            // 根据无功进行电流的变换
            if (!olpDataService.updateOrigin("OutLineIa", avc / VOLTAGE_115)) {
                log.warn("修改失败~~~   key: {}", "OutLineIa");
            }
            if (!olpDataService.updateOrigin("OutLineIb", avc / VOLTAGE_115)) {
                log.warn("修改失败~~~   key: {}", "OutLineIb");
            }
            if (!olpDataService.updateOrigin("OutLineIc", avc / VOLTAGE_115)) {
                log.warn("修改失败~~~   key: {}", "OutLineIc");
            }
            if (!olpDataService.updateOrigin("OutLineIa2", avc / VOLTAGE_115)) {
                log.warn("修改失败~~~   key: {}", "OutLineIa2");
            }
            if (!olpDataService.updateOrigin("OutLineIb2", avc / VOLTAGE_115)) {
                log.warn("修改失败~~~   key: {}", "OutLineIb2");
            }
            if (!olpDataService.updateOrigin("OutLineIc2", avc / VOLTAGE_115)) {
                log.warn("修改失败~~~   key: {}", "OutLineIc2");
            }
        }
    }

    @Scheduled(initialDelay = 5000, fixedRate = 3000)
    public void schedule() {
        for (IECData iecDatum : olpDataService.getIEC_DATA()) {
            if (iecDatum.getType().equals("yc")) {
                iecSendMessage.broadcastAnalogValues(iecServerService, updateIECData(iecDatum));
            } else if (iecDatum.getType().equals("yx")) {
                iecSendMessage.broadcastStatusValues(iecServerService, updateIECData(iecDatum));
            } else {
                log.warn("不属于遥信遥测，不执行指令发送。Data: {}", iecDatum);
            }
        }
        log.info("OLP01  有功: {}   无功： {}   Uab: {}", olpDataService.getKey("OutLinePower"),
                olpDataService.getKey("OutLineRepower"), olpDataService.getKey("OutLineUab"));
    }

    /**
     * 修改数据
     *
     * @param iecDatum : 数据
     * @return
     */
    private IECData updateIECData(IECData iecDatum) {
        if (iecDatum.getType().equals("yc")) {
            Double b = ((Math.random() * 2) - 1) * iecDatum.getChazhi();
            iecDatum.setValue(b + Float.parseFloat(iecDatum.getOriginValue().toString()));
        } else if (iecDatum.getType().equals("yx")) {
            iecDatum.setValue(Float.parseFloat(iecDatum.getOriginValue().toString()) == 1.0f ? 1 : 0);
        }
        return iecDatum;
    }


}
